06.JavaWeb Listener

Listener 基础

Listener 示例

package com.github.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author: subei
 * @Description: 统计网站在线人数 : 统计session
 */
public class OnlineCountListener implements HttpSessionListener {
    /**
     * 创建session监听: 看你的一举一动
     * 一旦创建Session就会触发一次这个事件!
     * @param se
     */
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();

        System.out.println(se.getSession().getId());

        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");

        if (onlineCount==null){
            onlineCount = new Integer(1);
        }else {
            int count = onlineCount.intValue();
            onlineCount = new Integer(count+1);
        }

        ctx.setAttribute("OnlineCount",onlineCount);
    }

    /**
     * 销毁session监听
     * 一旦销毁Session就会触发一次这个事件!
     * @param se
     */
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();

        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");

        if (onlineCount==null){
            onlineCount = new Integer(0);
        }else {
            int count = onlineCount.intValue();
            onlineCount = new Integer(count-1);
        }

        ctx.setAttribute("OnlineCount",onlineCount);
    }
    /**
     * Session销毁:
     * 1. 手动销毁  getSession().invalidate();
     * 2. 自动销毁
     */
}

web.xml 中注册监听器;

<!--注册监听器-->
<listener>
    <listener-class>com.github.listener.OnlineCountListener</listener-class>
</listener>